<template>
  <header class="header">
    <div class="container">
      <div class="header-left">
        <router-link to="/" class="logo">
          <span class="logo-icon">📚</span>
          <span class="logo-text">校园书籍循环</span>
        </router-link>
      </div>

      <nav class="nav">
        <router-link to="/" class="nav-link">首页</router-link>
        <router-link to="/books" class="nav-link">书籍市场</router-link>
        <router-link v-if="isLogin" to="/publish" class="nav-link">发布书籍</router-link>
      </nav>

      <div class="header-right">
        <template v-if="isLogin">
          <router-link to="/profile" class="user-info">
            <img :src="userAvatar" alt="头像" class="avatar" />
            <span class="username">{{ username }}</span>
          </router-link>
          <button @click="handleLogout" class="btn-logout">退出</button>
        </template>
        <template v-else>
          <router-link to="/login" class="btn-login">登录</router-link>
        </template>
      </div>
    </div>
  </header>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { logout } from '@/api/auth'

const router = useRouter()

// 用户登录状态
const isLogin = ref(false)
const username = ref('')
const userAvatar = ref('https://ui-avatars.com/api/?name=User&background=1890ff&color=fff')

// 检查登录状态
const checkLoginStatus = () => {
  const token = localStorage.getItem('token')
  const userInfo = localStorage.getItem('userInfo')
  
  if (token && userInfo) {
    isLogin.value = true
    const user = JSON.parse(userInfo)
    username.value = user.username || user.nickname || '用户'
    userAvatar.value = user.avatar || `https://ui-avatars.com/api/?name=${encodeURIComponent(username.value)}&background=1890ff&color=fff`
  } else {
    isLogin.value = false
  }
}

// 退出登录
const handleLogout = async () => {
  try {
    // 调用后端登出接口
    await logout()
  } catch (error) {
    console.error('登出错误:', error)
  } finally {
    // 无论后端是否成功，都清除本地存储
    localStorage.removeItem('token')
    localStorage.removeItem('userInfo')
    isLogin.value = false
    router.push('/login')
  }
}

// 监听登录事件
const handleUserLogin = () => {
  checkLoginStatus()
}

// 初始化检查登录状态
onMounted(() => {
  checkLoginStatus()
  // 监听登录事件
  window.addEventListener('user-login', handleUserLogin)
})

// 组件销毁时移除监听器
onUnmounted(() => {
  window.removeEventListener('user-login', handleUserLogin)
})
</script>

<style scoped lang="scss">
.header {
  background: #fff;
  border-bottom: 1px solid #e8e8e8;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  position: sticky;
  top: 0;
  z-index: 1000;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 64px;
}

.header-left {
  display: flex;
  align-items: center;
}

.logo {
  display: flex;
  align-items: center;
  text-decoration: none;
  color: #333;
  font-weight: 600;
  font-size: 18px;
  
  &:hover {
    opacity: 0.8;
  }
}

.logo-icon {
  font-size: 28px;
  margin-right: 8px;
}

.logo-text {
  color: #1890ff;
}

.nav {
  display: flex;
  align-items: center;
  gap: 30px;
}

.nav-link {
  text-decoration: none;
  color: #666;
  font-size: 15px;
  transition: color 0.3s;
  position: relative;
  
  &:hover {
    color: #1890ff;
  }
  
  &.router-link-active {
    color: #1890ff;
    font-weight: 500;
    
    &::after {
      content: '';
      position: absolute;
      bottom: -20px;
      left: 0;
      right: 0;
      height: 2px;
      background: #1890ff;
    }
  }
}

.header-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  text-decoration: none;
  color: #333;
  
  &:hover {
    opacity: 0.8;
  }
}

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
}

.username {
  font-size: 14px;
}

.btn-login,
.btn-logout {
  padding: 8px 20px;
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s;
}

.btn-login {
  background: #1890ff;
  color: #fff;
  border: none;
  text-decoration: none;
  display: inline-block;
  
  &:hover {
    background: #40a9ff;
  }
}

.btn-logout {
  background: transparent;
  color: #666;
  border: 1px solid #d9d9d9;
  
  &:hover {
    color: #1890ff;
    border-color: #1890ff;
  }
}
</style>
